package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.page.Pageable;
import com.ybkj.daijia.page.domain.Sort;
import com.ybkj.daijia.server.errand.ErrandTypeCompany;
import com.ybkj.daijia.server.errand.OrderType;
import com.ybkj.daijia.server.errand.OrderUp;
import com.ybkj.daijia.server.service.AreaService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.CouponService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.ErrandUpService;
import com.ybkj.daijia.server.service.FreightService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.OrderLocationManyService;
import com.ybkj.daijia.server.service.OrderService;
import com.ybkj.daijia.server.service.WeixinManagerService;
import com.ybkj.daijia.server.service.WeixinManagerXOrderService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.service.ZhuanxianService;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "errand/up")
public class ErrandUpController {

    @Autowired
    private ErrandUpService errandService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private WeixinManagerService weixinManagerService;

    @Autowired
    private WeixinManagerXOrderService weixinManagerXOrderService;

    @Autowired
    private DriverService driverService;

    @Autowired
    private ZhuanxianService zhuanxianService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private ZhuancheService zhuancheService;

    @Autowired
    private FreightService freightService;

    @Autowired
    private OrderLocationManyService orderLocationManyService;

    @Autowired
    private MemberService memberService;

    @RequestMapping("errandList")
    public ModelAndView errandList() {

        ModelAndView mv = new ModelAndView();
        return mv;

    }

    @RequestMapping("orderlistData")
    public @ResponseBody
    Page<Map<String, Object>> orderListData(
        @RequestParam(required = false, value = "orderNumber") String orderNumber,
        @RequestParam(required = false, value = "clientPhone") String clientPhone,
        @RequestParam(required = false, value = "driverPhone") String driverPhone,
        @RequestParam(required = false, value = "companyName") String companyName,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "errandOrderType") Long[] errandOrderType,
        @RequestParam(required = false, value = "endTime") String endTime,
        @RequestParam(required = false, value = "orderType") String[] orderType,
        @RequestParam(required = false, value = "status") Integer[] status,
        @RequestParam(required = false, value = "page") Integer page,
        @RequestParam(required = false, value = "rows") Integer rows,
        @RequestParam(required = false, value = "treatment") String treatment,
        @RequestParam(required = false, value = "startFinishTime") String startFinishTime,
        @RequestParam(required = false, value = "endFinishTime") String endFinishTime)
        throws ParseException {
        Page<Map<String, Object>> mapPage = new Page<Map<String, Object>>() {
            @Override
            public int getTotalPages() {
                return 0;
            }

            @Override
            public long getTotalElements() {
                return 0;
            }

            @Override
            public int getNumber() {
                return 0;
            }

            @Override
            public int getSize() {
                return 0;
            }

            @Override
            public int getNumberOfElements() {
                return 0;
            }

            @Override
            public List<Map<String, Object>> getContent() {
                return null;
            }

            @Override
            public boolean hasContent() {
                return false;
            }

            @Override
            public Sort getSort() {
                return null;
            }

            @Override
            public boolean isFirst() {
                return false;
            }

            @Override
            public boolean isLast() {
                return false;
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public boolean hasPrevious() {
                return false;
            }

            @Override
            public Pageable nextPageable() {
                return null;
            }

            @Override
            public Pageable previousPageable() {
                return null;
            }

            @Override
            public Iterator<Map<String, Object>> iterator() {
                return null;
            }
        };
        return mapPage;

    }

    @RequestMapping(value = "exportOrder", method = RequestMethod.GET)
    public void exportOrder(
        @RequestParam(required = false, value = "orderNumber") String orderNumber,
        @RequestParam(required = false, value = "clientPhone") String clientPhone,
        @RequestParam(required = false, value = "driverPhone") String driverPhone,
        @RequestParam(required = false, value = "companyName") String companyName,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "errandOrderType") String errandOrderType[],
        @RequestParam(required = false, value = "endTime") String endTime,
        @RequestParam(required = false, value = "orderType") String[] orderType,
        @RequestParam(required = false, value = "status") Integer[] status,
        @RequestParam(required = false, value = "treatment") String treatment,
        @RequestParam(required = false, value = "startFinishTime") String startFinishTime,
        @RequestParam(required = false, value = "endFinishTime") String endFinishTime,
        HttpServletRequest req, HttpServletResponse response
    ) throws IOException, ParseException {

    }


    @RequestMapping(value = {"list", ""}, method = RequestMethod.GET)
    public ModelAndView list(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "param") String param,
        @RequestParam(required = false, value = "start") String startTime,
        @RequestParam(required = false, value = "orderType") String orderType,
        @RequestParam(required = false, value = "end") String endTime) throws ParseException {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    @RequestMapping(value = "refuselist", method = RequestMethod.GET)
    public ModelAndView refuselist(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "param") String param,
        @RequestParam(required = false, value = "start") String startTime,
        @RequestParam(required = false, value = "end") String endTime) throws ParseException {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    /**
     * 已完成订单列表
     *
     * @param companyId
     * @param param
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "finishlist", method = RequestMethod.GET)
    public ModelAndView finishlist(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "param") String param,
        @RequestParam(required = false, value = "start") String startTime,
        @RequestParam(required = false, value = "orderType") String orderType,
        @RequestParam(required = false, value = "end") String endTime) throws ParseException {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    /**
     * 已销单列表
     *
     * @param companyId
     * @param param
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "cancellist", method = RequestMethod.GET)
    public ModelAndView cancellist(
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam(required = false, value = "param") String param,
        @RequestParam(required = false, value = "start") String startTime,
        @RequestParam(required = false, value = "orderType") String orderType,
        @RequestParam(required = false, value = "end") String endTime) throws ParseException {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    /**
     * 销单
     *
     * @param id
     * @param cause 销单原因
     * @return
     */
    @RequestMapping(value = "cancel", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> cancel(Long id, String cause) {

        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    /**
     * 召回(收回订单)
     *
     * @param id
     * @param cause
     * @return
     */
    @RequestMapping(value = "callback", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> callback(Long id, String cause) {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    //订单详情
    @RequestMapping(value = "detail/{id}", method = RequestMethod.GET)
    public ModelAndView detail(@PathVariable("id") Long id) {
        ModelAndView mv = new ModelAndView();
        return mv;
    }

    //修改订单金额
    @RequestMapping(value = "updateRealMoney", method = RequestMethod.POST)
    public synchronized @ResponseBody
    Result<OrderUp> updateRealMoney(
        Long id, BigDecimal couponMoney, BigDecimal cashMoney, BigDecimal signMoney,
        BigDecimal balanceMoney,
        BigDecimal alipayMoney, BigDecimal weixinMoney, String content,
        BigDecimal driverMoney, Integer driverPlusMinus,
        BigDecimal memberMoney, Integer memberPlusMinus) {
        Result<OrderUp> vo = new Result<OrderUp>();
        return vo;
    }

    /**
     * 评价订单
     *
     * @param id
     * @param content
     * @param score
     * @return
     */
    @RequestMapping(value = "review", method = RequestMethod.POST)
    public String review(Long id, String content, String score) {
        return "";
    }

    /**
     * 订单添加备注
     *
     * @param id
     * @param memo
     * @return
     */
    @RequestMapping(value = "remark", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> remark(Long id,
        @RequestParam(required = false, value = "memo") String memo) {
        Map<String, Object> result = new HashMap<String, Object>();
        return result;
    }

    /**
     * 根据距离和时间按照公司的收费标准预估订单价格
     *
     * @param time
     * @param mileage
     * @return
     */
    @RequestMapping(value = "getPrice", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> getPrice(
        @RequestParam("areaId") Long areaId,
        @RequestParam("areaWeightId") Long areaWeightId,
        @RequestParam(required = false, value = "weight") String weight,
        @RequestParam(required = false, value = "goodsCash") String goodsCash,
        @RequestParam(required = false, value = "thanksCash") String thanksCash,
        @RequestParam(required = false, value = "protectPrice") String protectPrice,
        @RequestParam(required = false, value = "couponId") Long couponId,
        @RequestParam(required = false, value = "time") String time,
        @RequestParam(required = false, value = "mileage") String mileage) {
        Map<String, Object> result = new HashMap<String, Object>();
        return result;
    }

//	@RequestMapping(value="listpaidan",method=RequestMethod.GET)
//	public @ResponseBody List<Driver> listpaidan(Long orderId){
//		
//		List<Driver> list=driverService.listErrandDrivers(orderId);
//		if(!CollectionUtils.isEmpty(list)){
//			for (Driver driver : list) {
//				driver.setCompany(companyService.findOne(driver.getCompanyId()));
//				driver.setPaotuiAmount(errandService.countBusyAmount(driver.getId()));//errandGrabAmount最大抢单数量
//			}
//		}
//		
//		return list;
//		
//	}

    //追加
    @RequestMapping(value = "addmore/{id}", method = RequestMethod.GET)
    public ModelAndView addmore(HttpSession session, @PathVariable("id") Long id) {
//
        return new ModelAndView();
    }

    @RequestMapping(value = "paidan/{id}", method = RequestMethod.GET)
    public ModelAndView mapmode(@PathVariable("id") Long id) {
//
        return new ModelAndView();
    }

    //派单
    @RequestMapping(value = "paidan", method = RequestMethod.POST)
    public @ResponseBody
    Result<OrderUp> paidan(Long driverId, Long orderId) {

        Result<OrderUp> result = new Result<OrderUp>();
        return result;
    }

    /**
     * 后台添加订单跳转页面
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView add(HttpSession session) {
//
        return new ModelAndView();
    }

    /**
     * 后台直接创建跑腿订单
     *
     * @throws ParseException
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> create(
        @RequestParam("passengerId") Long passengerId,
        @RequestParam("passengerName") String passengerName,
        @RequestParam("passengerPhone") String passengerPhone,
        @RequestParam(required = false, value = "companyId") Long companyId,
        @RequestParam("startAddress") String startAddress,
        @RequestParam(required = false, value = "startDetailed") String startDetailed,
        @RequestParam("startLat") Double startLat,
        @RequestParam("startLng") Double startLng,
        @RequestParam("content") String content,
        @RequestParam(required = false, value = "startTime") String startTime,
        @RequestParam(required = false, value = "deliverAddress") String deliverAddress,
        @RequestParam(required = false, value = "endDetailed") String endDetailed,
        @RequestParam(required = false, value = "deliverLat") Double deliverLat,
        @RequestParam(required = false, value = "deliverLng") Double deliverLng,
        @RequestParam(required = false, value = "orderTypeId") Long orderTypeId,
        @RequestParam(required = false, value = "imgs") String imgs,
        @RequestParam(required = false, value = "startPrice") Double startPrice,
        @RequestParam(required = false, value = "mileagePrice") Double mileagePrice,
        @RequestParam(required = false, value = "travelTimePrice") Double travelTimePrice,
        @RequestParam(required = false, value = "travelTime") Integer travelTime,
        @RequestParam(required = false, value = "mileage") Double mileage,
        @RequestParam(required = false, value = "shouldPay") Double shouldPay,
        @RequestParam(required = false, value = "goodsCash") Double goodsCash,
        @RequestParam(required = false, value = "thanksCash") Double thanksCash,
        @RequestParam(required = false, value = "protectPrice") Double protectPrice,
        @RequestParam(required = false, value = "weight") Double weight,
        @RequestParam(required = false, value = "weightPrice") Double weightPrice,
        @RequestParam(required = false, value = "otherServices") String otherServices,
        @RequestParam(required = false, value = "couponId") Long couponId) throws ParseException {

        Map<String, Object> result = new HashMap<String, Object>();
        return result;
    }

    @RequestMapping(value = "orderTypeList", method = RequestMethod.GET)
    public @ResponseBody
    List<OrderType> orderTypeList(
        @RequestParam(required = false, value = "companyId") Long companyId) throws ParseException {
        List<OrderType> list = new LinkedList<>();
        return list;
    }

    /**
     * 跑腿升级类型页面
     *
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "orderTypeUp", method = RequestMethod.GET)
    public ModelAndView orderTypeUp() throws ParseException {
        return new ModelAndView();
    }

    /**
     * 查询指定公司的跑腿类型
     *
     * @param companyId
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "queryErrandTypes", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> queryErrandTypes(
        @RequestParam("companyId") Long companyId) throws ParseException {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    //查询跑腿类型根据类型名称
    @RequestMapping(value = "orderTypeDataUp", method = RequestMethod.GET)
    public @ResponseBody
    Page<Map<String, Object>> orderTypeData(
        @RequestParam(required = false, value = "typeName") String typeName
    ) throws ParseException {
        Page<Map<String, Object>> mapPage = new Page<Map<String, Object>>() {
            @Override
            public int getTotalPages() {
                return 0;
            }

            @Override
            public long getTotalElements() {
                return 0;
            }

            @Override
            public int getNumber() {
                return 0;
            }

            @Override
            public int getSize() {
                return 0;
            }

            @Override
            public int getNumberOfElements() {
                return 0;
            }

            @Override
            public List<Map<String, Object>> getContent() {
                return null;
            }

            @Override
            public boolean hasContent() {
                return false;
            }

            @Override
            public Sort getSort() {
                return null;
            }

            @Override
            public boolean isFirst() {
                return false;
            }

            @Override
            public boolean isLast() {
                return false;
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public boolean hasPrevious() {
                return false;
            }

            @Override
            public Pageable nextPageable() {
                return null;
            }

            @Override
            public Pageable previousPageable() {
                return null;
            }

            @Override
            public Iterator<Map<String, Object>> iterator() {
                return null;
            }
        };
        return mapPage;
    }

    /**
     * 查询单个跑腿类型.
     *
     * @param id 类型id.
     * @return
     */
    @RequestMapping(value = "findOrderType", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> findOrderType(
        @RequestParam(required = true, value = "id") Long id) {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    //添加跑腿类型
    @RequestMapping(value = "createOrderType", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> createOrderType(
        @RequestParam(required = true, value = "typeName") String typeName,
        @RequestParam(required = true, value = "typeSeq") Integer typeSeq,
        @RequestParam(required = false, value = "errandOrderTypeDetaileds") String errandOrderTypeDetaileds,
        @RequestParam(required = false, value = "typepicture") String typepicture,
        @RequestParam(required = false, value = "otherServices") String otherServices,
        @RequestParam(required = false, value = "tips") String tips,
        @RequestParam(required = false, value = "allowTips") String allowTips) {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    @RequestMapping(value = "updateOrderType", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateOrderType(
        @RequestParam(required = true, value = "id") Long id,
        @RequestParam(required = false, value = "typeName") String typeName,
        @RequestParam(required = false, value = "typeSeq") Integer typeSeq,
        @RequestParam(required = false, value = "errandOrderTypeDetaileds") String errandOrderTypeDetaileds,
        @RequestParam(required = false, value = "typepicture") String typepicture,
        @RequestParam(required = false, value = "otherServices") String otherServices,
        @RequestParam(required = false, value = "tips") String tips,
        @RequestParam(required = false, value = "allowTips") String allowTips)
        throws ParseException {

        Map<String, Object> map = new HashMap<String, Object>();
        return map;

    }

    @RequestMapping(value = "deleteOrderType", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> deleteOrderType(
        @RequestParam("typeId") Long typeId,
        @RequestParam("typeName") String typeName) {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    @RequestMapping(value = "pointDetails", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> pointDetails(Long id) {

        Map<String, Object> map = new HashMap<String, Object>();
        return map;

    }

    /**
     * 开通公司
     *
     * @param typeId
     * @param companyId
     * @param companyName
     * @param mileAreaId
     * @param weightAreaId
     * @param mileAreaName
     * @param weightAreaName
     * @param ifPrepay
     * @param payType
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "openCompany", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> openCompany(
        @RequestParam(required = true, value = "typeId") Long typeId,
        @RequestParam(required = true, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "companyName") String companyName,
        @RequestParam(required = true, value = "mileAreaId") Long mileAreaId,
        @RequestParam(required = true, value = "weightAreaId") Long weightAreaId,
        @RequestParam(required = true, value = "mileAreaName") String mileAreaName,
        @RequestParam(required = true, value = "weightAreaName") String weightAreaName,
        @RequestParam(required = true, value = "ifPrepay") String ifPrepay,
        @RequestParam(required = true, value = "payType") String payType) throws ParseException {
//		Subject subject = SecurityUtils.getSubject();
//		if (subject.isPermitted("errand")) {
//			Map<String,Object> map=new HashMap<String, Object>();
//			map = errandService.openCompany(typeId,companyId,companyName,
//					mileAreaId,weightAreaId,ifPrepay,payType,mileAreaName,weightAreaName);
//
//			return map;
//		}else{
//			throw new NoPermissionException();
//		}
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    @RequestMapping(value = "queryErrandTypeCompany", method = RequestMethod.GET)
    public @ResponseBody
    Page<ErrandTypeCompany> queryErrandTypeCompany(
        @RequestParam(required = true, value = "typeId") Long typeId,
        @RequestParam(required = false, value = "companyName") String companyName)
        throws ParseException {
//		Subject subject = SecurityUtils.getSubject();
//		if (subject.isPermitted("errand")) {
//			Page<ErrandTypeCompany> page =errandService.queryErrandTypeCompany(typeId,companyName);
//			return page;
//		}else{
//			throw new NoPermissionException();
//		}
        Page<ErrandTypeCompany> page = new Page<ErrandTypeCompany>() {
            @Override
            public int getTotalPages() {
                return 0;
            }

            @Override
            public long getTotalElements() {
                return 0;
            }

            @Override
            public int getNumber() {
                return 0;
            }

            @Override
            public int getSize() {
                return 0;
            }

            @Override
            public int getNumberOfElements() {
                return 0;
            }

            @Override
            public List<ErrandTypeCompany> getContent() {
                return null;
            }

            @Override
            public boolean hasContent() {
                return false;
            }

            @Override
            public Sort getSort() {
                return null;
            }

            @Override
            public boolean isFirst() {
                return false;
            }

            @Override
            public boolean isLast() {
                return false;
            }

            @Override
            public boolean hasNext() {
                return false;
            }

            @Override
            public boolean hasPrevious() {
                return false;
            }

            @Override
            public Pageable nextPageable() {
                return null;
            }

            @Override
            public Pageable previousPageable() {
                return null;
            }

            @Override
            public Iterator<ErrandTypeCompany> iterator() {
                return null;
            }
        };
        return page;
    }

    @RequestMapping(value = "updateOpenCompany", method = RequestMethod.POST)
    public @ResponseBody
    Map<String, Object> updateCarTypeCompany(
        @RequestParam(required = true, value = "typeId") Long typeId,
        @RequestParam(required = true, value = "companyId") Long companyId,
        @RequestParam(required = true, value = "companyName") String companyName,
        @RequestParam(required = true, value = "mileAreaId") Long mileAreaId,
        @RequestParam(required = true, value = "weightAreaId") Long weightAreaId,
        @RequestParam(required = true, value = "mileAreaName") String mileAreaName,
        @RequestParam(required = true, value = "weightAreaName") String weightAreaName,
        @RequestParam(required = true, value = "ifPrepay") String ifPrepay,
        @RequestParam(required = true, value = "payType") String payType) throws ParseException {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }

    @RequestMapping(value = "deleteTypeCompany", method = RequestMethod.GET)
    public @ResponseBody
    Map<String, Object> deleteTypeCompany(
        @RequestParam("typeCompanyId") Long typeCompanyId) throws ParseException {
        Map<String, Object> map = new HashMap<String, Object>();
        return map;
    }
}

